package com.chat.controller.user;

import com.chat.dto.EmployeeCheckDTO;
import com.chat.dto.EmployeeDTO;
import com.chat.dto.EmployeeLoginDTO;
import com.chat.entity.Employee;
import com.chat.result.Result;
import com.chat.service.EmployeeService;
import com.chat.utils.AliOssUtil;
import com.chat.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

// 管理端
@RestController
@RequestMapping("user")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private AliOssUtil aliOssUtil;

    //员工端登录
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<Employee> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
//        String token = JwtUtil.createJWT(
//                jwtProperties.getAdminSecretKey(),
//                jwtProperties.getAdminTtl(),
//                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getStuNumber())
//                .token(token)
                .build();


        return Result.success(employee);
    }

    //注册校验
    @PostMapping("/registerCheck")
    @ApiOperation("注册校验")
    public Result registerCheck(@RequestBody EmployeeCheckDTO employeeCheckDTO) {
        log.info("注册校验：{}",employeeCheckDTO);
        String result = employeeService.registerCheck(employeeCheckDTO);
        log.info("结果：{}",result);
        if (result == null) {
            return Result.success();
        }else {
            return Result.error(result);
        }

    }

    //员工端注册
    //新增员工
    @PostMapping("/register")
    @ApiOperation("员工注册")
    public Result addEmployee(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}",employeeDTO);
        employeeService.addEmployee(employeeDTO);
        return Result.success();
    }

    //上传头像
    @PostMapping("/upload")
    @ApiOperation("上传头像")
    public Result upload(MultipartFile file){
        log.info("上传头像：{}",file);
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            //文件的请求路径
            String imageUrl = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(imageUrl);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return Result.error("文件上传失败！");
    }

}
